// 用户路由
const express = require("express");
// 引入connect-multiparty中间件
var multipart = require("connect-multiparty");
// 创建中间件对象
var multipartMiddleware = multipart();
// 创建路由对象
const user = express.Router();
// 引入数据库配置
const db = require("../mysql");

// 获取所有用户接口
user.get("/getUser", multipartMiddleware, (req, res) => {
  //   console.log(req.query);
  const sqlStr = `select * from users where phone like '%${req.query.query}%' or username like '%${req.query.query}%' or school like '%${req.query.query}%'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: error });
    res.json({ status: 200, data: results });
  });
});

// 根据id获取用户接口
user.get("/getUserById/:id", multipartMiddleware, (req, res) => {
  //   console.log(req.params);
  const sqlStr = `select * from users where id='${req.params.id}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: err });
    res.json({ status: 200, data: results[0] });
  });
});

// 根据昵称获取用户信息接口
user.get("/getUserByName/:name",multipartMiddleware, (req, res) => {
  // console.log(req.params);
  const sqlStr = `select * from users where username='${req.params.name}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: err });
    res.json({ status: 200, data: results[0] });
  });
})

// 添加用户接口
user.post("/addUser", multipartMiddleware, (req, res) => {
  console.log(req.body);
  const sqlStr = `insert into users (phone,password,username,school,email) values ('${req.body.phone}','${req.body.password}','${req.body.username}','${req.body.school}','${req.body.email}')`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: "插入失败" });
    res.json({ status: 200, message: "插入成功" });
  });
});

// 修改用户接口
user.put("/editUser/:id", multipartMiddleware, (req, res) => {
  //   console.log(req.params);
  //   console.log(req.body);
  const sqlStr = `update users set username='${req.body.username}',password='${req.body.password}',school='${req.body.school}' WHERE id='${req.params.id}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: "修改失败" });
    res.json({ status: 200, message: "修改成功" });
  });
});

// 删除用户接口
user.post("/deleteUser", multipartMiddleware, (req, res) => {
  console.log(req.body);
  const sqlStr = `delete from users where id='${req.body.id}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ status: 404, message: "删除失败" });
    res.json({ status: 200, message: "删除成功" });
  });
});

// 根据用户名修改用户密码
user.post("/changePwd", multipartMiddleware, (req, res) => {
  console.log(req.body)
  const sqlStr = `update users set password='${req.body.password}' where username='${req.body.username}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 400, message: "修改密码失败" });
    res.json({ code: 200, message: "修改密码成功" });
  });
});
// 根据邮箱修改用户密码
user.post("/changePwdByEmail", multipartMiddleware, (req, res) => {
  console.log(req.body)
  const sqlStr = `update users set password='${req.body.newPassWord}' where email='${req.body.email}'`;
  db.query(sqlStr, (err, results) => {
    if (err) return res.json({ code: 400, message: "重置密码失败" });
    res.json({ code: 200, message: "重置密码成功" });
  });
});

module.exports = user;
